/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "mpu6050.h"
#include "OLED.h"
#include "w25q64.h"
#include "elog.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
void StartMPU6050Task(void *argument);
osThreadId_t mpu6050TaskHandle;
const osThreadAttr_t mpu6050Task_attributes = {
        .name = "mpu6050Task",
        .stack_size = 128 * 4,
        .priority = (osPriority_t) osPriorityNormal,
};

void StartW25Q64Task(void *argument);
osThreadId_t w25q64TaskHandle;
const osThreadAttr_t w25q64Task_attributes = {
        .name = "w25q64Task",
        .stack_size = 128 * 4,
        .priority = (osPriority_t) osPriorityNormal,
};
/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
    /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
    /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
    /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
    /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
    /* add threads, ... */
//    mpu6050TaskHandle = osThreadNew(StartMPU6050Task, NULL, &mpu6050Task_attributes);
    w25q64TaskHandle = osThreadNew(StartW25Q64Task, NULL, &w25q64Task_attributes);
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
    /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
    /* Infinite loop */
    for(;;)
    {
        HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_1);
        osDelay(500);
    }
  /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void StartMPU6050Task(void *argument)
{

    i2c_interrupt_init();
    mpu6050_init();

    while(1)
    {

        MPU6050_acquire();

        {
            OLED_ShowSignedNum(2, 1, MPU6050_GetData().Accel_X, 5);
            OLED_ShowSignedNum(3, 1, MPU6050_GetData().Accel_Y, 5);
            OLED_ShowSignedNum(4, 1, MPU6050_GetData().Accel_Z, 5);
            OLED_ShowSignedNum(2, 8, MPU6050_GetData().Gyro_X, 5);
            OLED_ShowSignedNum(3, 8, MPU6050_GetData().Gyro_Y, 5);
            OLED_ShowSignedNum(4, 8, MPU6050_GetData().Gyro_Z, 5);
        }
        osDelay(10);

    }
}
uint8_t w25qID[2];
void StartW25Q64Task(void *argument)
{
    uint32_t w25q_state = W25Qx_OK;
    uint8_t pData[4] = {0x47,0x43,0x54,0x47};
    uint8_t readDate[4];
    w25q_state = BSP_W25Qx_Init();
    if(W25Qx_OK == w25q_state)
    {
        log_i("init error");
    }

    BSP_W25Qx_Read_ID(w25qID);
    OLED_ShowHexNum(2,1,w25qID[0],2);
    OLED_ShowHexNum(2,3,w25qID[1],2);

//    w25q_state = BSP_W25Qx_WriteEnable();
//    w25q_state = BSP_W25Qx_Write(pData,0x00000000,4);
//    if(W25Qx_OK == w25q_state)
//    {
//        log_i("BSP_W25Qx_Write error");
//    }
    w25q_state = BSP_W25Qx_Read(readDate,0x00,4);
    if(W25Qx_OK == w25q_state)
    {
        log_i("BSP_W25Qx_Read error");
    }

    OLED_ShowHexNum(3,1,readDate[0],2);
    OLED_ShowHexNum(3,3,readDate[1],2);
    OLED_ShowHexNum(3,5,readDate[2],2);
    OLED_ShowHexNum(3,7,readDate[3],2);
    while(1)
    {
        osDelay(10);
    }
}

/* USER CODE END Application */

